Mehr ggplot2!

library(tidyverse)

gapminder_dat_full <- readRDS(here::here("data", "gapminder_dat.rds"))

gapminder_dat_trend <- gapminder_dat_full %>% 
  filter(time > 1990, time < 2022, country %in% c("nga", "zaf", "deu", "rus", "chn", "ind", "bra", "usa", "egy", "aus", "mex", "jpn")) 

Facetting

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  theme_bg()

Lösung: Faceting

Faceting

Anordnen von einer einzelnen Variable in einem Raster:

facet_wrap()

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  facet_wrap(vars(country), nrow = 4) +
  theme_bg()

facet_grid

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  facet_grid(country ~ .) +
  theme_bg()

Facetting - Mehrere Variablen

Anordnen von mehreren Variable in einem Raster:

facet_wrap()

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  facet_wrap(vars(country, world_4region), nrow = 4) +
  theme_bg()

facet_grid()

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_point() + 
  geom_line() +
  facet_grid(country ~ world_4region) +
  theme_bg()

Facetting - Tipps

Plot alle Punkte

bg <- gapminder_dat_trend %>%
  mutate(country_bg = country) %>%
  select(-country)

ggplot(gapminder_dat_trend, aes(x = time, y = co2_pcap_cons, color = country, group = country)) +
  # background lines: drawn in every facet, grouped by country_bg
  geom_line(
    data = bg,
    aes(x = time, y = co2_pcap_cons, group = country_bg),
    inherit.aes = FALSE,
    color = "grey70",
    alpha = 0.5,
    linewidth = 0.4
  ) +
  # foreground points/lines for the focal country in each facet
  geom_line(linewidth = 0.6) +
  geom_line(linewidth = 1.5) +
  facet_wrap(vars(country)) +
  guides(color = "none") +
  theme_bg()

Facetting - Tipps

Plot Mittelwerte

ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_line(linewidth = 1) +
  geom_smooth(aes(group = income_groups), color = "grey") +
  facet_wrap(vars(income_groups)) +
  theme_bg()

Sortieren

Sortieren, läuft in ggplot2 generell über factor():

gapminder_dat_trend$income_groups_fac <- factor(gapminder_dat_trend$income_groups, 
                                               levels = c("lower_middle_income", "upper_middle_income", "high_income")
                                               )
ggplot(data = gapminder_dat_trend, 
       mapping = aes(x = time, 
                     y = co2_pcap_cons, 
                     color = country)) +
  geom_line(linewidth = 1) +
  geom_smooth(aes(group = income_groups), color = "grey") +
  facet_wrap(vars(income_groups_fac)) +
  theme_bg()

Scales

“Scales in ggplot2 control the mapping from data to aesthetics. They take your data and turn it into something that you can see, like size, colour, position or shape.” ggplot2: Elegant Graphics for Data Ana

Legenden

Legenden werden automatisch erzeugt. Dafür werden die aestetics genutzt, also das mapping von Daten zu grafischen Elementen.

Koordinatensytem

  • Polar, evtl. characters plot oder weltraumplot als beispiel

Abspeichern

Vektor vs Raster (Rolfs 7)